package com.bafa.objects;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Iterator;
import com.bafa.objects.team.Team;
import com.bafa.dbcache.GrabSeasons;
import com.bafa.util.comparators.MatchByDateComparator;
import com.bafa.util.comparators.RoundsComparator;

public class Season implements Serializable{

	private boolean completed;
	private TreeMap<Integer,Round> cupRounds = new TreeMap();
	private TreeMap<Integer,Division> divisions = new TreeMap();
	private int ID;
	private boolean league;
	private TreeMap<Integer,Round> plateRounds = new TreeMap();
	private String season;
	private boolean switchedOn;
	private Team[] teamsNotAdded;
	

	private int year;
	
	public Season(){
		
	}
	public Season(com.bafa.objects.schema.Season season){
		this(season.getID(),season.getSeason(),season.getYear(),season.isLeague(),season.isSwitchedOn());
		for(int i=0;i<season.getDivision().size();i++){
			addDivision(new Division(season.getDivision().get(i)));
		}
		for(int i=0;i<season.getCupRound().size();i++){
			addCupRound(new Round(season.getCupRound().get(i)));
		}
		for(int i=0;i<season.getPlateRound().size();i++){
			addPlateRound(new Round(season.getPlateRound().get(i)));
		}
	}
	
	public Season(int ID, String season, int year, boolean cupLeague,boolean switchedOn){
		this.ID=ID;
		this.season=season;
		this.year=year;
		this.league = cupLeague;
		this.switchedOn = switchedOn;
		
		
	}
	public void addCupRound(Round round){
		cupRounds.put(round.getID(),round);
	}
	
	
	
	
	public void addDivision(Division division){
		divisions.put(division.getDivision(),division);
		
	}
	public void addPlateRound(Round round){
		plateRounds.put(round.getID(),round);
	}
	public boolean containsDivision(Integer division){
		return divisions.containsKey(division);
	}
	public Round[] cupRoundsArray(){
		return cupRounds.values().toArray(new Round[cupRounds.size()]);
	}
	public Division[] divisionArray(){
		return divisions.values().toArray(new Division[divisions.size()]);
	}
	
	

	public boolean equals(Season season){
		return this.getID() == season.getID();
	}
	public ArrayList getAllDisputedResults(){
		Match[] matches = null;
		ArrayList arraylist = new ArrayList();
		for(int i=1;i<=divisions.size();i++){
			Division div = divisions.get(new Integer(i));
			matches = div.disputedResultsArray();
			arraylist.add(matches);
		}
		
		return arraylist;
	}
	
	public ArrayList getAllDueMatches(){
		Match[] matches = null;
		ArrayList arraylist = new ArrayList();
		for(int i=1;i<=divisions.size();i++){
			Division div = divisions.get(new Integer(i));
			matches = div.dueMatchArray();
			arraylist.add(matches);
		}
		Iterator tr = cupRounds.values().iterator();
		while(tr.hasNext()){
			Round round = (Round)tr.next();
			Match[] allMatches = round.matchArray();
			TreeSet list = new TreeSet(new MatchByDateComparator());
			for(int i=0;i<allMatches.length;i++){
				Match match = allMatches[i];
				if(match.isResultDue()){
					list.add(match);
				}
			}
			arraylist.add((Match[])list.toArray(new Match[list.size()]));
		}
		Iterator tr1 = plateRounds.values().iterator();
		while(tr1.hasNext()){
			Round round = (Round)tr1.next();
			Match[] allMatches = round.matchArray();
			TreeSet list = new TreeSet(new MatchByDateComparator());
			for(int i=0;i<allMatches.length;i++){
				Match match = allMatches[i];
				if(match.isResultDue()){
					list.add(match);
				}
			}
			arraylist.add((Match[])list.toArray(new Match[list.size()]));
		}
		return arraylist;
	}
	
	public ArrayList getAllMatches(){
		Match[] matches = null;
		ArrayList arraylist = new ArrayList();
		for(int i=1;i<=divisions.size();i++){
			Division div = divisions.get(i);
			matches = div.matchArray();
			arraylist.add(matches);
		}
		return arraylist;
	}
	public ArrayList getAllUnconfirmedMatches(){
		Match[] matches = null;
		ArrayList arraylist = new ArrayList();
		for(int i=1;i<=divisions.size();i++){
			Division div = divisions.get(new Integer(i));
			matches = div.playedUnconfirmedMatchesArray();
			arraylist.add(matches);
		}
		return arraylist;
	}
	
	public TreeMap getCupRounds() {
		return cupRounds;
	}
	public TreeMap getDivisions() {
		return divisions;
	}
	
	public int getID() {
		return ID;
	}
	
	public TreeMap getPlateRounds() {
		return plateRounds;
	}

	public String getSeason() {
		if(season.equals("cup"))
			season = "Cup";
		return season;
	}
	
	public Team[] getTeamsNotAdded() {
		return teamsNotAdded;
	}

	public int getYear() {
		return year;
	}
	
	public boolean isCompleted() {
		return completed;
	}

	public boolean isLeague() {
		return league;
	}
	
	public boolean isSwitchedOn() {
		return switchedOn;
	}

	public Round[] plateRoundsArray(){
		return plateRounds.values().toArray(new Round[plateRounds.size()]);
	}

	public void setCompleted(boolean completed) {
		this.completed = completed;
	}

	public String toString(){
		return "Divisions: "+divisions.size();
	}
	
	
}
